﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HZ.Supervise.IBLL;
using HZ.Supervise.BLL;
using HZ.Supervise.Model;
using System.Runtime.Remoting.Messaging;

/// <summary>
///CurrentAccount 的摘要说明
/// </summary>

public class CurrentAccount
{
    //系统Session Key常量 
    private const string ACCOUNT = "ACCOUNT_KEY";
    private const string VALIDATE_CODE = "NUM_CODE";  

    //系统超级管理员,该用户用户最高权限且不允许删除
    private const string SUPER_ADMIN = "root";

    //常用的功能页面地址
    private const string URL_INDEX = "~/login.aspx";
    //监管系统注销返回登录窗口地址
    private static string URL_SIGNIN = ConfigurationManager.AppSettings["LinkAddress"];
    //登陆中心账户导航页
    public static string URL_INDEXPAGE = ConfigurationManager.AppSettings["LinkIndex"];
    //private const string URL_SIGNIN = "~/login.aspx";
    private const string URL_TIMEOUT = "~/Common/TimeoutPage.aspx";
    private const string URL_NO_PERMISSION = "~/Common/NoPermission.aspx";// "~/CommonPage/NoPermission.aspx";  

    //各用户类型默认的内容主页
    public const string URL_DEFAULT_INDEX_URL = "~/CommonPage/defaultContent.aspx";
    public const string URL_CENTER_INDEX_URL = "~/Admin/defaultContent.aspx";
    public const string URL_HOSPITAL_INDEX_URL = "~/Hospitals/defaultContent.aspx";
    public const string URL_ENTERPRISE_INDEX_URL = "~/Enterprise/defaultContent.aspx";


    static CurrentAccount()
    {


    }

    /// <summary>
    /// 获取当前用户的Id
    /// </summary>
    public static string UserID
    {
        get
        {
            return GetUser().UserID;
        }
    }

    /// <summary>
    /// 获取当前用户的用户名
    /// </summary>
    public static string UserName
    {
        get
        {
            return GetUser().UserName;
        }
    }

    /// <summary>
    /// 当前登录用户的IP
    /// </summary>
    public static string ClientIP
    {
        get
        {
            return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
    }

    /// <summary>
    /// 是否超时
    /// </summary>
    /// <returns></returns>
    public static bool IsTimeout()
    {
        //    默认登陆用户 仅在开发模式中使用 生产环境应将其去掉
        //Model.Org org = new HZ.Supervise.Model.Org();
        //org.OrgID = "h01";
        //org.OrgName = "江苏省人民医院";
        //Model.Role role = new HZ.Supervise.Model.Role();
        //role.RoleID = "01";
        //role.RoleName = "医疗机构";
        //HZ.Supervise.Model.User model = new HZ.Supervise.Model.User();
        //model.Org = org;
        //model.UserRole = role;
        //model.UserID = "y001";
        //model.UserName = "江苏省人民医院";

        //Model.Org org = new HZ.Supervise.Model.Org();
        //org.OrgID = "01";
        //org.OrgName = "江苏华招医药有限公司";
        //Model.Role role = new HZ.Supervise.Model.Role();
        //role.RoleID = "02";
        //role.RoleName = "配送企业";
        //HZ.Supervise.Model.User model = new HZ.Supervise.Model.User();
        //model.Org = org;
        //model.UserRole = role;
        //model.UserID = "P001";
        //model.UserName = "江苏华招医药有限公司";
        //if (HttpContext.Current.Session[ACCOUNT] == null)
        //{
        //    HttpContext.Current.Session[ACCOUNT] = model;
        //}

        HZ.Supervise.Model.User myAccount = (HZ.Supervise.Model.User)HttpContext.Current.Session[ACCOUNT];
        if (myAccount == null)
        {
            return true;
        }
        return false;
    }
     

    /// <summary>
    /// 获取当前用户实体
    /// </summary>
    /// <returns></returns>
    public static HZ.Supervise.Model.User GetUser()
    {

        User myAccount = (User)HttpContext.Current.Session[ACCOUNT];

        if (myAccount == null)
        {

            Goto_Timeout();
            return null;
        }
        return myAccount;

    }

    /// <summary>
    /// 将当前登陆用户保存到Session
    /// </summary>
    /// <param name="model"></param>
    public static void SetUser(HZ.Supervise.Model.User model)
    {
        HttpContext.Current.Session[ACCOUNT] = model;
    }

    /// <summary>
    /// 退出登陆
    /// </summary>
    public static void Logout()
    {
        //
        HttpContext.Current.Session.Clear();
        //
        HttpContext.Current.Session.Abandon();
    }

    /// <summary>
    /// 重新登陆
    /// </summary>
    public static void Goto_Login()
    {
        Logout();
        //清除缓存
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.CacheControl = "no-cache";

        //跳转到登陆页面

        // BLL层缓存清除
        //HZ.Supervise.IBLL.IBLL_Login ibl = HZ.Supervise.BLLFactory.Factory.BLL_Login();
        //ibl.Logoff();

        //HZ.Supervise.IBLL.IBLL_User iu = HZ.Supervise.BLLFactory.BLL_User();

        HttpContext.Current.Response.Redirect("~/login.aspx", true);
    }

    /// <summary>
    /// 判断当前用户是否系统超级管理员
    /// </summary>
    /// <param name="strUserName"></param>
    /// <returns></returns>
    public static bool IsSuperAdmin()
    {
        if (GetUser().UserID.ToLower() == SUPER_ADMIN.ToLower())
        {
            return true;
        }
        return false;
    }

    /// <summary>
    /// 判断给定用户是否系统超级管理员
    /// </summary>
    /// <param name="userID"></param>
    /// <returns></returns>
    public static bool IsSuperAdmin(string userID)
    {
        if (userID.ToLower() == SUPER_ADMIN.ToLower())
        {
            return true;
        }
        return false;
    }

    /// <summary>
    /// 跳转到没有权限页面
    /// </summary>
    public static void Goto_NoPermission()
    {
        HttpContext.Current.Response.Redirect(URL_NO_PERMISSION, true);
    }

    /// <summary>
    /// 跳转到超时页面
    /// </summary>
    public static void Goto_Timeout()
    {
        //清除缓存
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.CacheControl = "no-cache";
        //
        HttpContext.Current.Response.Redirect(URL_TIMEOUT, true);
    }

    /// <summary>
    /// 验证码校验函数
    /// </summary>
    /// <param name="code"></param>
    /// <returns></returns>
    public static bool VerifyValidateCode(string code)
    {
        if (!string.IsNullOrEmpty(code))
        {
            if (HttpContext.Current.Session[VALIDATE_CODE] != null)
            {
                if (HttpContext.Current.Session[VALIDATE_CODE].ToString() == code)
                {
                    //验证成功清空Session
                    HttpContext.Current.Session[VALIDATE_CODE] = null;
                    return true;
                }

            }
        }
        //验证失败清空Session
        HttpContext.Current.Session[VALIDATE_CODE] = null;
        return false;
    }

    /// <summary>
    /// 设置验证码
    /// </summary>
    /// <param name="code"></param>
    public static void SetValidateCode(string code)
    {
        HttpContext.Current.Session[VALIDATE_CODE] = code;
    }

    /// <summary>
    /// 获取当前部署的区域
    /// </summary>
    /// <returns></returns>
    public static HZ.Supervise.Model.Area GetCurrentDeployArea()
    {
        //IBLL.IBLL_Area areaCtrl = HZ.Supervise.BLLFactory.Factory.BLL_Area();

        HZ.Supervise.IBLL.IBLL_Area areaCtrl = HZ.Supervise.BLLFactory.IBLL_Area();
        return areaCtrl.GetArea(Constants.CURRENT_DEPLOY_AREAID);
    }
}
